import { v4 as uuidv4 } from 'uuid';
import BasicTool from './BasicTool';

/**
 * 点位处理工具类
 */
export default class MarkerTool {
  /**
   *
   * @param {*} viewer
   */
  constructor(viewer) {
    this.viewer = viewer;
    this.basicTool = new BasicTool(viewer);
  }

  /**
   * 地图加载一个点
   *
   * @param {*} x 经度
   * @param {*} y 纬度
   * @param {*} z 海拔
   * @return {*}
   */
  generatePoint(x, y, z) {
    var pt = new Cesium.Cartesian3.fromDegrees(Number(x ? x : 0), Number(y ? y : 0), Number(z ? z : 0));
    var result = this.viewer.entities.add(pt);
    return result;
  }

  /**
   * 生成线
   * @param {*} points [[x,y,z]]格式
   * @param {*} line   -- 绑定的线对象 自定义
   * @returns
   */
  generatePolyline(points, line) {
    var positions = points.map((p) => this.basicTool.createPoint(p[0], p[1], p[2]));
    var bData = {
      polyline: {
        layerId: 'polyline' + uuidv4(),
        positions: positions,
        show: true,
        width: 3,
        material: new Cesium.PolylineDashMaterialProperty({
          color: Cesium.Color.YELLOW,
        }),
        depthFailMaterial: new Cesium.PolylineDashMaterialProperty({
          color: Cesium.Color.YELLOW,
        }),
      },
    };
    var result = this.viewer.entities.add(bData);
    result.line = line;
    return result;
  }

  /**
   * 创建PolygonGraphics .具体选项参考[PolygonGraphics ]{@link http://cesium.xin/cesium/cn/Documentation1.72/PolygonGraphics.html?classFilter=Polygon}
   * @param {*} points points [[x,y,z]]格式
   * @returns
   */
  generatePolylineGraphic(points,labelName,id) {
    var positions = points.map((p) => this.basicTool.createPoint(p[0], p[1], p[2]));
    var entitie = {
      id:id,            
      polygon: {
        hierarchy: new Cesium.PolygonHierarchy(positions),
        extrudedHeightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        material: Cesium.Color.YELLOW.withAlpha(0.4),
        clampToGround: true,
      },
      polyline: {
        positions: positions,
        clampToGround: true, //贴地
        width: 3,
        material: new Cesium.PolylineDashMaterialProperty({
          color: Cesium.Color.YELLOW,
        }),
        depthFailMaterial: new Cesium.PolylineDashMaterialProperty({
          color: Cesium.Color.YELLOW,
        }),
      },
    };
    var polygon = this.viewer.entities.add(entitie);

    var polyPositions = polygon.polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions;
    var polyCenter = Cesium.BoundingSphere.fromPoints(polyPositions).center; //中心点
    polyCenter = Cesium.Ellipsoid.WGS84.scaleToGeodeticSurface(polyCenter);

    polygon.position = polyCenter;
    // 这种方式删除地块的时候会把label一起删除
    polygon.label={
      text: labelName,
        fillColor: Cesium.Color.fromCssColorString('#ffffff'),
        font: '32px',
        horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
        verticalOrigin: Cesium.VerticalOrigin.TOP,
        scaleByDistance: new Cesium.NearFarScalar(0, 1, 1200, 1),
        distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 10000),
        eyeOffset: new Cesium.Cartesian3(0, 0, -100),// 悬浮
        pixelOffset: new Cesium.Cartesian2(0, -40), //  悬浮
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        disableDepthTestDistance: Number.POSITIVE_INFINITY,
    }
    // 这种方式添加的label删除地块时，不会同步删除
    // this.viewer.entities.add({
    //   id: id,
    //   position: polyCenter,
    //   label: {
    //     text: labelName,
    //     fillColor: Cesium.Color.fromCssColorString('#ffffff'),
    //     font: '32px',
    //     horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
    //     verticalOrigin: Cesium.VerticalOrigin.TOP,
    //     scaleByDistance: new Cesium.NearFarScalar(0, 1, 1200, 1),
    //     distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 10000),
    //     // eyeOffset: new Cesium.Cartesian3(0, 0, -100),
    //     // pixelOffset: new Cesium.Cartesian2(0, -40),
    //     heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
    //     disableDepthTestDistance: Number.POSITIVE_INFINITY,
    //   },
    // });
    return polygon;
  }

  /**
   * 创建图片Billboard对象
   *
   * @param {*} url 图片url
   * @param {*} width
   * @param {*} height
   * @return {*}
   */
  createPictureBillboard(url, width, height) {
    var billboardGp = new Cesium.BillboardGraphics({
      image: url,
      width: width ? width : 40,
      height: height ? height : 40,
      disableDepthTestDistance: Number.POSITIVE_INFINITY,
      verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
      horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
      heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
    });
    return billboardGp;
  }

  /**
   * 创建图标点位
   * @param {*} url 图片url
   * @param {*} width 图片宽度
   * @param {*} height 图片高度
   * @param {*} lon 经度
   * @param {*} lat 纬度
   * @param {*} alt 海拔
   * @param {*} device 关联的对象 需要时可用
   * @returns
   */
  createPicMarker(url, width, height, lon, lat, alt, device) {
    var billboardGp = this.createPictureBillboard(url, width, height);
    var position = this.basicTool.createPoint(lon, lat, alt);
    var opt = {
      billboard: billboardGp,
      position: position,
      // point: new Cesium.PointGraphics ({
      //   show : true,
      //   pixelSize : 10,
      //   heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,  //设置HeightReference高度参考类型为CLAMP_TO_GROUND贴地类型
      //   color : new Cesium.Color (255,255,0,1),
      // }),
      // label: {
      //   show: true,
      //   showBackground: false,
      //   text: device.name || '',
      //   scale: 0.5, //这里非常巧妙的先将字体大小放大一倍在缩小一倍
      //   font: 'normal bold 32px MicroSoft YaHei',
      //   pixelOffset: new Cesium.Cartesian2(0, -65),
      //   eyeOffset: new Cesium.Cartesian3(0, 0, -65),
      //   // fillColor: Cesium.Color.fromCssColorString("#d6f50a"),
      //   disableDepthTestDistance: Number.POSITIVE_INFINITY,
      //   horizontalOrigin : Cesium.HorizontalOrigin.CENTER,
      //   position: position,
      // },
    };
    var entity = new Cesium.Entity(opt);
    entity.device = device;
    // this.addEntity(entity, this.layers.deviceLayer);
    this.viewer.entities.add(entity);
    return entity;
  }

  /**
   * 设置图片
   * @param {*} entity
   * @param {*} url 图片url
   */
  setPicMarkerSymbol(entity, url) {
    entity.billboard = this.createPictureBillboard(url, 30, 30);
  }

  /**
   * 设置图标文本
   * @param {*} entity
   * @param {*} text
   * @returns
   */
  setLabelText(entity, text) {
    var label = this.createLabelGraphic({
      show: true,
      text: text,
      scale: 0.5, //这里非常巧妙的先将字体大小放大一倍在缩小一倍
      font: 'normal bold 32px MicroSoft YaHei',
      pixelOffset: new Cesium.Cartesian2(0, -65),
      eyeOffset: new Cesium.Cartesian3(0, 0, -65),
      // fillColor: Cesium.Color.fromCssColorString("#d6f50a"),
      showBackground: true,
      disableDepthTestDistance: Number.POSITIVE_INFINITY,
    });
    entity.label = label;
    return label;
  }

  /**
   *清除当前所有
   */
  clean() {
    this.viewer.entities.removeAll();
  }
}
